<template>
  <div>
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="名称">
        <el-input
          v-model="searchForm.sfName"
          placeholder="输入员工名称回车即可"
          clearable
          @keydown.enter.native="getPage()"
        ></el-input>
      </el-form-item>
      <el-form-item label="年龄">
        <el-input
          v-model="searchForm.sfAge"
          placeholder="输入年龄回车即可"
          clearable
          @keydown.enter.native="getPage()"
        ></el-input>
      </el-form-item>

      <el-form-item label="性别">
        <el-select v-model="searchForm.sfSex" placeholder="筛选">
          <el-option label="男" value="男"></el-option>
          <el-option label="女" value="女"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="showFrom">添加员工</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="sfName" label="名称" width="100" />
      <el-table-column prop="sfUser" label="账号" width="120" />
      <el-table-column prop="sfSex" label="性别" width="80" />
      <el-table-column prop="sfAge" label="年龄" width="80" />
      <el-table-column prop="sfAddress" label="住址" />
      <el-table-column prop="sfEntry" label="入职时间" />
      <el-table-column prop="sfTel" label="电话" width="120" />
      <el-table-column label="账号状态" width="80">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.sfStatus"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            @change="status(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="showRoles(scope.row)"
            >分配角色</el-button
          >
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :page-size="limit"
      :current-page="page"
      :page-sizes="[2, 10, 20, 30]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form" label-position="right">
        <el-form :model="form">
          <el-form-item label="名称" label-width="100px">
            <el-input v-model="form.sfName" autocomplete="off" />
          </el-form-item>
          <el-form-item label="账号" label-width="100px">
            <el-input v-model="form.sfUser" autocomplete="off" />
          </el-form-item>
          <el-form-item label="性别" label-width="100px">
            <el-input v-model="form.sfSex" autocomplete="off" />
          </el-form-item>
          <el-form-item label="年龄" label-width="100px">
            <el-input v-model="form.sfAge" autocomplete="off" />
          </el-form-item>
          <el-form-item label="住址" label-width="100px">
            <el-input v-model="form.sfAddress" autocomplete="off" />
          </el-form-item>
          <el-form-item label="入职时间" label-width="100px">
            <el-date-picker
              v-model="form.sfEntry"
              type="datetime"
              placeholder="选择日期时间"
              align="right"
              :picker-options="pickerOptions"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addORupdate">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="分配角色" :visible.sync="RolesVisible">
      <el-transfer
        v-model="value"
        filterable
        :data="data"
        :titles="['全部角色', '已有角色']"
        :filter-method="filterMethod"
      ></el-transfer>
      <div slot="footer" class="dialog-footer">
        <el-button @click="RolesVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRoles">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // props: {
      //   key: "rid",
      //   label: `ptType`,
      // },
      RolesVisible: false,
      data: [],
      value: [],
      searchForm: {},
      tableData: [],
      staff: {},
      page: 1,
      limit: 10,
      total: 0,
      dialogFormVisible: false,
      title: "添加分店",
      form: {},
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
        ],
      },
      value1: "",
      value2: "",
      value3: "",
      sfId: null,
    };
  },
  created() {
    this.getPage();
  },
  computed: {},
  watch: {
    searchForm: {
      deep: true,
      handler(val) {
        if (val.sfName == "") {
          val.sfName = null;
        }
        if (val.sfAge == "") {
          val.sfAge = null;
        }
      },
    },
  },
  methods: {
    setRoles() {
      if (this.sfId == null) {
        return;
      }
      let roleIds = this.$http
        .post(`http://127.0.0.1/position/setByStaffId/${this.sfId}`, {
          roleIds: this.value,
        })
        .then(({ data }) => {
          this.$message({
            type: "success",
            message: "保存成功",
          });
          this.RolesVisible = false;
        });
    },
    filterMethod(query, item) {
      return item.label.indexOf(query) > -1;
    },
    showRoles(row) {
      this.data = [];
      this.RolesVisible = true;
      this.sfId = row.sfId;
      this.$http.get(`http://127.0.0.1/position/all`).then(({ data }) => {
        this.$http
          .get(`http://127.0.0.1/position/getByStaffId/${row.sfId}`)
          .then((res) => {
            let rids = res.data.positionList.map((elemnt) => {
              return elemnt.rid;
            });
            data.positionList.forEach((p) => {
              this.data.push({
                key: p.rid,
                label: p.ptType,
              });
            });
            this.value = rids;
          });
      });
    },
    search() {
      this.getPage();
    },
    status(row) {
      this.$http
        .post(`http://127.0.0.1/staff/status/${row.sfId}`)
        .then(({ data }) => {
          this.getPage();
        });
    },
    showFrom() {
      this.form = {};
      this.dialogFormVisible = true;
    },
    getPage() {
      this.$http
        .post(
          `http://127.0.0.1/staff/page/${this.page}/${this.limit}`,
          this.searchForm
        )
        .then(({ data }) => {
          this.tableData = data.pageInfo.list;
          this.total = data.pageInfo.total;
        });
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getPage();
    },
    handleSizeChange(val) {
      this.limit = val;
      this.getPage();
    },
    handleEdit(indedx, row) {
      this.$http
        .get(`http://127.0.0.1/staff/getById/${row.sfId}`)
        .then(({ data }) => {
          this.dialogFormVisible = true;
          this.form = data.staff;
        });
    },
    handleDelete(indedx, row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http
            .delete(`http://127.0.0.1/staff/delete/${row.sfId}`)
            .then(({ data }) => {
              this.getPage();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addORupdate() {
      if (this.form.sfId != null) {
        this.$http
          .post(`http://127.0.0.1/staff/updateById`, this.form)
          .then(({ data }) => {
            this.getPage();
            this.dialogFormVisible = false;
          });
      } else {
        this.$http
          .put(`http://127.0.0.1/staff/save`, this.form)
          .then(({ data }) => {
            this.getPage();
            this.dialogFormVisible = false;
          });
      }
    },
  },
};
</script>
<style  scoped>
</style>